const path = require('path')
const webpack = require('webpack')
const CompressionPlugin = require('compression-webpack-plugin')
const zlib = require('zlib')
const createThemeColorReplacerPlugin = require('./config/plugin.config')

function resolve(dir) {
  return path.join(__dirname, dir)
}
/* 
const cdn = {
  css: [],
  js: [
    'https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
    'https://cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js',
    'https://cdn.jsdelivr.net/npm/vuex@3.1.1/dist/vuex.min.js',
    'https://cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js'
  ]
} */

/* const isProd = process.env.NODE_ENV === 'production'

const assetsCDN = {
  // webpack build externals
  externals: {
    vue: 'Vue',
    'vue-router': 'VueRouter',
    vuex: 'Vuex',
    axios: 'axios'
  },
} */
// vue.config.js
let timeStamp = new Date().getTime(); // 最新版本号时间戳
const vueConfig = {
  /*   chainWebpack: config => {
      config.module
        .rule('eslint')
        .use('eslint-loader')
        .loader('eslint-loader')
        .tap(options => {
          options.fix = true
          return options
        })
    }, */
  configureWebpack: {
    // webpack plugins
    plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),


      //vue EventBus

      // 压缩成 .gz 文件
      /* new CompressionPlugin({
        filename: '[path][base].gz',
        algorithm: 'gzip',
        test: /\.js$|\.css$|\.html$/,
        threshold: 10240,
        minRatio: 0.8
      }),
      // 压缩成 .br 文件，如果 zlib 报错无法解决，可以注释这段使用代码，一般本地没问题，需要注意线上服务器会可能发生找不到 zlib 的情况。
      new CompressionPlugin({
        filename: '[path][base].br',
        algorithm: 'brotliCompress',
        test: /\.(js|css|html|svg)$/,
        compressionOptions: {
          params: {
            [zlib.constants.BROTLI_PARAM_QUALITY]: 11
          }
        },
        threshold: 10240,
        minRatio: 0.8
      }) */
    ],
    output: { // 输出重构 打包编译后的js文件名称,添加时间戳.
      filename: `static/js/js[name].${timeStamp}.js`,
      chunkFilename: `static/js/chunk.[id].${timeStamp}.js`,
    },
    // if prod, add externals
    externals: {}//isProd ? assetsCDN.externals : {}
  },

  chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))

    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .oneOf('inline')
      .resourceQuery(/inline/)
      .use('vue-svg-icon-loader')
      .loader('vue-svg-icon-loader')
      .end()
      .end()
      .oneOf('external')
      .use('file-loader')
      .loader('file-loader')
      .options({
        name: 'assets/[name].[hash:8].[ext]'
      })

    // if prod is on
    // assets require on cdn
    /* if (isProd) {
      config.plugin('html').tap(args => {
        args[0].cdn = cdn
        return args
      })
    } */
  },

  css: {
    extract: { // 打包后css文件名称添加时间戳
      filename: `static/css/[name].${timeStamp}.css`,
      chunkFilename: `static/css/chunk.[id].${timeStamp}.css`,
    },
    loaderOptions: {
      less: {
        modifyVars: {
          // less vars，customize ant design theme

          // 'primary-color': '#F5222D',
          // 'link-color': '#F5222D',
          // 'border-radius-base': '4px'
        },
        // DO NOT REMOVE THIS LINE
        javascriptEnabled: true
      }
    }
  },
  runtimeCompiler: true,//解决自定义模版
  publicPath: '/',
  outputDir: process.env.VUE_APP_DIST,
  assetsDir: 'static',
  lintOnSave: false,
  productionSourceMap: false,
  devServer: {
    port: 8081,
    // host:'localhost',
    open: true, // 项目运行时自动在浏览器打开
    hotOnly: false, //  构建失败时回退
    headers: {
      "Access-Control-Allow-Origin": "*",
    },
    // https://apifox.com/apidoc/shared-98fd392c-d203-4d70-80b2-7e960c0f5e05 人资接口文档
    // https://www.pmdaniu.com/clouds/156444/2ce078a5f98b2d732ad480e1481865c6-38543/start.html?_d=Tue%20Jul%2002%202024%2010:55:39%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)#id=a1t8d1&p=%E5%8E%9F%E5%9E%8B%E4%BF%AE%E8%AE%A2%E8%AE%B0%E5%BD%95 人资原型

    // 本地 http://192.168.0.104:8086(吴) http://192.168.0.109:8086(郑)
    // 预测试 http://120.77.240.246:82/hrs
    // 正式环境 https://hr.taxdata.top

    proxy: {
      '/hrs': {
        target: process.env.NODE_ENV === 'development' ? "http://120.77.240.246:82/hrs" : "",
        changeOrigin: true,//是否跨域
        // logLevel: 'debug',
        // onProxyReq: (proxyReq, req) => {
        //   // http请求
        //   console.log('[HPM] %s %s %s %s', req.method, req.originalUrl, '->', req.url);
        // },
        pathRewrite: { //匹配路径
          "^/hrs": ''
        }
      }
    }
  }
}

// preview.pro.loacg.com only do not use in your production;
if (process.env.VUE_APP_PREVIEW === 'true') {
  // add `ThemeColorReplacer` plugin to webpack plugins
  vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
}

module.exports = vueConfig
